package cn.io0288.ks;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class ContactUtils {
    // 联系人工具类
    public ArrayList<ContactsBean> localContactsList = null;
    public ArrayList<ContactsBean> remoteContactsList = null;
    public ArrayList<ContactsBean> tempContactsList = null;
    public ArrayList<ContactsBean> newContactsList = null;

    public static void addContacts(Context context, ContactsBean contacts) {
        ContentResolver contentResolver = context.getContentResolver();

        // 创建空联系人
        ContentValues values = new ContentValues();

        // 插入原始联系人
        values.put(ContactsContract.RawContacts.ACCOUNT_TYPE, (String) null);
        values.put(ContactsContract.RawContacts.ACCOUNT_NAME, (String) null);

        long curi = ContentUris.parseId(Objects.requireNonNull(contentResolver.insert(ContactsContract.RawContacts.CONTENT_URI, values)));
        // 多号码处理
        for (String p:contacts.phone) {
            ContentValues phoneValues = new ContentValues();
            phoneValues.put(ContactsContract.Data.RAW_CONTACT_ID, curi);
            phoneValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);

            phoneValues.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
            phoneValues.put(ContactsContract.CommonDataKinds.Phone.NUMBER, p);

            contentResolver.insert(ContactsContract.Data.CONTENT_URI, phoneValues);
        }
        // 修改联系人名
        ContentValues nameValues = new ContentValues();
        nameValues.put(ContactsContract.Data.RAW_CONTACT_ID, curi);
        nameValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);

        nameValues.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, contacts.name);

        contentResolver.insert(ContactsContract.Data.CONTENT_URI, nameValues);
    }

    public static ArrayList<ContactsBean> getAllContacts(Context context) {
        ArrayList<ContactsBean> contacts = new ArrayList<>();
        ContentResolver mContentResolver = context.getContentResolver();

        Cursor nameCursor = mContentResolver.query(
                ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
        while (nameCursor.moveToNext()) {
            //新建一个联系人实例
            ContactsBean temp = new ContactsBean(nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.Contacts._ID)));

            //获取联系人姓名
            temp.name = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            //获取联系人电话号码
            Cursor phoneCursor = mContentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + temp._ID, null, null);
            List<String> phones = new ArrayList<>();
            while (phoneCursor.moveToNext()) {
                // 多个电话号码处理
                String phone = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                phones.add(phone.replace("-", "").replace(" ", ""));
            }
            // 转回数组
            temp.phone = phones.toArray(new String[phones.size()]);

            //获取联系人备注信息
            Cursor noteCursor = mContentResolver.query(
                    ContactsContract.Data.CONTENT_URI,
                    new String[]{
                        ContactsContract.Data._ID, ContactsContract.CommonDataKinds.Nickname.NAME
                    },
                ContactsContract.Data.CONTACT_ID + "=?" + " AND " +
                        ContactsContract.Data.MIMETYPE + "='" +
                        ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE + "'",
                        new String[]{temp._ID}, null);
            if (noteCursor.moveToFirst()) {
                do {
                    temp.note = noteCursor.getString(noteCursor.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.NAME));
                    Log.i("note:", temp.note);
                } while (noteCursor.moveToNext());
            }

            // 获取联系人在通讯录中的Uri
            Cursor lookupCursor = mContentResolver.query(ContactsContract.Contacts.CONTENT_URI,
                    new String[]{ContactsContract.Contacts.LOOKUP_KEY},
                    ContactsContract.Contacts._ID + " = ?",
                    new String[]{temp._ID},
                    null);
            if (lookupCursor.moveToFirst()) {
                temp.lookupKey = lookupCursor.getString(0);
                temp.setTag(ContactsBean.TAG_LOCAL);
            }

            contacts.add(temp);
            //记得要把cursor给close掉
            phoneCursor.close();
            noteCursor.close();
            lookupCursor.close();
        }
        nameCursor.close();
        return contacts;
    }
}
